package com.autumn.workflow.boot;

import org.activiti.engine.impl.persistence.StrongUuidGenerator;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.DataSourceProcessEngineAutoConfiguration;
import org.activiti.spring.boot.JpaProcessEngineAutoConfiguration;
import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.autumn.workflow.command.diagram.DiagramCommand;
import com.autumn.workflow.command.diagram.ProcessTraceCommand;
import com.autumn.workflow.command.task.FreedomTaskCompleteCommand;
import com.autumn.workflow.command.task.InitializeUserTaskNodeAndFlowCommand;
import com.autumn.workflow.command.task.QueryNextPossibleUserTaskNodesCommand;
import com.autumn.workflow.service.WorkflowService;
import com.autumn.workflow.service.impl.ActivitiWorkflowService;

@Configuration
@EnableConfigurationProperties({WorkflowProperties.class})
@AutoConfigureAfter({JpaProcessEngineAutoConfiguration.class, DataSourceProcessEngineAutoConfiguration.class})
public class WorkflowAutoConfiguration implements ProcessEngineConfigurationConfigurer {

    private final WorkflowProperties properties;

    public WorkflowAutoConfiguration(WorkflowProperties properties) {
        this.properties = properties;
    }

    @Override
    public void configure(SpringProcessEngineConfiguration configuration) {
        if (null != properties.getActivityFontName()) {
            configuration.setActivityFontName(properties.getActivityFontName());
        }
        if (null != properties.getLabelFontName()) {
            configuration.setLabelFontName(properties.getLabelFontName());
        }
        if (null != properties.getAnnotationFontName()) {
            configuration.setAnnotationFontName(properties.getAnnotationFontName());
        }

        configuration.setIdGenerator(new StrongUuidGenerator());
    }

    @Bean
    @ConditionalOnMissingBean(WorkflowService.class)
    public WorkflowService activitiWorkflowService() {
        return new ActivitiWorkflowService();
    }

    @Bean
    @ConditionalOnMissingBean(InitializeUserTaskNodeAndFlowCommand.class)
    public InitializeUserTaskNodeAndFlowCommand initializeUserTaskNodeAndFlowCommand() {
        return new InitializeUserTaskNodeAndFlowCommand();
    }

    @Bean
    @ConditionalOnMissingBean(QueryNextPossibleUserTaskNodesCommand.class)
    public QueryNextPossibleUserTaskNodesCommand queryNextPossibleUserTaskNodesCommand() {
        return new QueryNextPossibleUserTaskNodesCommand();
    }

    @Bean
    @ConditionalOnMissingBean(FreedomTaskCompleteCommand.class)
    public FreedomTaskCompleteCommand freedomTaskCompleteCommand() {
        return new FreedomTaskCompleteCommand();
    }

    @Bean
    @ConditionalOnMissingBean(DiagramCommand.class)
    public DiagramCommand diagramCommand() {
        return new DiagramCommand();
    }

    @Bean
    @ConditionalOnMissingBean(ProcessTraceCommand.class)
    public ProcessTraceCommand processTraceCommand() {
        return new ProcessTraceCommand();
    }
}
